using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using RadioTimer.Services;
using System.Collections.ObjectModel;

namespace RadioTimer.ViewModels
{
    /// <summary>
    /// 日志页面视图模型
    /// </summary>
    public partial class LogPageViewModel : BaseViewModel
    {
        private readonly ILogService _logService;

        /// <summary>
        /// 日志条目集合
        /// </summary>
        public ObservableCollection<LogEntry> LogEntries => _logService.LogEntries;

        /// <summary>
        /// 日志条目数量
        /// </summary>
        public int LogCount => LogEntries.Count;

        public LogPageViewModel(ILogService logService)
        {
            _logService = logService;

            // 订阅日志条目变化事件，更新日志数量显示
            _logService.LogEntries.CollectionChanged += (s, e) => 
            {
                OnPropertyChanged(nameof(LogCount));
            };
        }

        /// <summary>
        /// 清空日志命令
        /// </summary>
        [RelayCommand]
        private void ClearLogs()
        {
            _logService.ClearLogs();
        }

        /// <summary>
        /// 刷新命令
        /// </summary>
        [RelayCommand]
        private void Refresh()
        {
            OnPropertyChanged(nameof(LogCount));
        }
    }
}